﻿using System;
using System.Collections.Generic;
using System.Text;
using Wjtour.Common;
using Wjtour.Domain;
using System.Linq;
using System.Data.Common;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Data;

namespace Wjtour.BLL
{
    public class article_bll : base_dll
    {
        public article GetArticleByTitle(string title)
        {
            string sql = string.Format("select * from article t where t.title = '{0}'", Utility.GetSafeSql(title));
            return DbAccess.GetObjects<article>(sql).FirstOrDefault();
        }

        public Wjtour.Domain.article GetFirstArticleWithImage(string type, int? isaudit)
        {
            string where = " where 1 = 1 ";
            if (isaudit.HasValue)
                where += " and a.isaudit =  " + isaudit;
            if (!string.IsNullOrEmpty(type))
                where += " and a.cid in (" + type + ")";
            where += " and a.img is not null and a.img <> '' ";

            string sql = string.Format(@"select top {0} a.* from article a
                {1}
                order by a.id desc", 1, where);
            return GetAll<Wjtour.Domain.article>(sql).ToList().FirstOrDefault();
        }

        public List<Wjtour.Domain.article> GetAuditTopList(string type, int top, int? isaudit)
        {
            string where = " where 1=1";
            if (isaudit.HasValue)
                where += " and a.isaudit =  " + isaudit;

            if (!string.IsNullOrEmpty(type))
                where += " and a.cid in (" + type + ")";

            string sql = string.Format(@"select top {0} a.* from article a
                {1}
                order by a.id desc", top, where);
            return GetAll<Wjtour.Domain.article>(sql).ToList();
        }

        public List<Wjtour.Domain.article> GetAuditTopListWithLogo(string type, int top, int? isaudit)
        {
            string where = " where 1=1 and a.img is not null and a.img<>''";
            if (isaudit.HasValue)
                where += " and a.isaudit =  " + isaudit;

            if (!string.IsNullOrEmpty(type))
                where += " and a.cid in (" + type + ")";

            string sql = string.Format(@"select top {0} a.* from article a
                {1}
                order by a.id desc", top, where);
            return GetAll<Wjtour.Domain.article>(sql).ToList();
        }

        public List<Wjtour.Domain.article> GetAuditTopListWithFirstLogo(string type, int top, int? isaudit)
        {
            string sql = string.Empty;

            string where = " where 1=1";
            if (!string.IsNullOrEmpty(type))
                where += " and a.cid in (" + type + ")";
            if (isaudit.HasValue)
                where += " and a.isaudit =  " + isaudit;

            if (top <= 1)
            {
                sql = string.Format("select top 1 from article a {0}", where);
            }
            else
            {
                int n = top - 1;
                sql = string.Format(@"select * from article a 
                    inner join (
	                    select top 1 id,img from article 
	                    where img is not null and img<>''
	                    order by id desc
	                    union
	                    select top {0} a.id,a.img from article a
	                    {1}
	                    order by id desc
                    )T on a.id =T.id", n, where);
            }
            return GetAll<Wjtour.Domain.article>(sql).ToList();
        }

        /// <summary>
        /// 获取某个会员发布的某种类型的文章
        /// </summary>
        /// <param name="type"></param>
        /// <param name="memberid"></param>
        /// <param name="page"></param>
        /// <param name="pagesize"></param>
        /// <returns></returns>
        public PaginationData<Wjtour.Domain.article> GetList(string type, int memberid, int page, int pagesize = 20)
        {
            string where = " where 1=1";
            if (!string.IsNullOrEmpty(type))
                where += " and a.cid in (" + type + ")";
            if (memberid > 0)
                where += " and a.memberid =" + memberid;

            string sql = string.Format(@"select a.* from article a
                {0}
                order by a.id desc", where);
            PaginationData<Wjtour.Domain.article> data = GetPaginationData<Wjtour.Domain.article>(sql, page, pagesize);
            return data;
        }

        public PaginationData<Wjtour.Domain.article> GetList(string type, int page, int pagesize = 20)
        {
            string where = " where 1=1";
            if (!string.IsNullOrEmpty(type))
                where += " and a.cid in (" + type + ")";

            string sql = string.Format(@"select a.* from article a
                {0}
                order by a.id desc", where);
            PaginationData<Wjtour.Domain.article> data = GetPaginationData<Wjtour.Domain.article>(sql, page, pagesize);
            return data;
        }

        //        public List<Wjtour.Domain.article> GetList(string type)
        //        {
        //            string where = " where 1=1";
        //            if (!string.IsNullOrEmpty(type))
        //                where += " and a.cid in (" + type + ")";

        //            string sql = string.Format(@"select a.* from article a
        //                {0}
        //                order by a.id desc", where);
        //            return DbAccess.GetObjects<Wjtour.Domain.article>(sql).ToList();
        //        }

        public PaginationData<Wjtour.Domain.dto.article_articletype> GetList(string q, string type, int page, int pagesize = 20)
        {
            string where = " where 1=1";
            if (!string.IsNullOrEmpty(q))
                where += " and a.title like '%" + Utility.GetSafeSql(q) + "%' ";
            if (!string.IsNullOrEmpty(type))
                where += " and at.id in (" + type + ")";

            string sql = string.Format(@"select a.*,at.title articletype,at.tag from article a
                inner join articletype at on a.cid=at.id
                {0}
                order by a.id desc", where);
            PaginationData<Wjtour.Domain.dto.article_articletype> data = GetPaginationData<Wjtour.Domain.dto.article_articletype>(sql, page, pagesize);
            return data;
        }

        public Wjtour.Domain.article Load(int id)
        {
            string sql = "select * from article where id=" + id;
            return DbAccess.GetObjects<Wjtour.Domain.article>(sql).FirstOrDefault();
        }

        public bool Save(Wjtour.Domain.article entity)
        {
            string sql = "";

            DbCommand cmd = DbAccess.CreateCommand();

            if (entity.id > 0)
            {
                sql = @"UPDATE article SET 
                cid = @cid,
                title = @title,
                img = @img,
                source = @source,
                content = @content,
                hits = @hits,
                sort = @sort,
                keyword = @keyword,
                [desc] = @desc,
                addtime = @addtime,
                link = @link,
                author = @author,
                zan = @zan,
                memberid = @memberid,
                isaudit = @isaudit
                 WHERE id=@id";
                DbAccess.AddInParameter(cmd, "@id", DbType.Int32, entity.id);

            }
            else
            {
                sql = @"INSERT INTO article( 
                cid,title,img,source,content,hits,sort,keyword,[desc],addtime,link,author,zan,memberid,isaudit)VALUES(
                @cid,@title,@img,@source,@content,@hits,@sort,@keyword,@desc,@addtime,@link,@author,@zan,@memberid,@isaudit)";
            }
            DbAccess.AddInParameter(cmd, "@cid", DbType.Int32, entity.cid);
            DbAccess.AddInParameter(cmd, "@title", DbType.String, entity.title);
            DbAccess.AddInParameter(cmd, "@img", DbType.String, entity.img);
            DbAccess.AddInParameter(cmd, "@source", DbType.String, entity.source);
            DbAccess.AddInParameter(cmd, "@content", DbType.String, entity.content);
            DbAccess.AddInParameter(cmd, "@hits", DbType.Int32, entity.hits);
            DbAccess.AddInParameter(cmd, "@sort", DbType.Int32, entity.sort);
            DbAccess.AddInParameter(cmd, "@keyword", DbType.String, entity.keyword);
            DbAccess.AddInParameter(cmd, "@desc", DbType.String, entity.desc);
            DbAccess.AddInParameter(cmd, "@addtime", DbType.DateTime, entity.addtime);
            DbAccess.AddInParameter(cmd, "@link", DbType.String, entity.link);
            DbAccess.AddInParameter(cmd, "@author", DbType.String, entity.author);
            DbAccess.AddInParameter(cmd, "@zan", DbType.Int32, entity.zan);
            DbAccess.AddInParameter(cmd, "@memberid", DbType.Int32, entity.memberid);
            DbAccess.AddInParameter(cmd, "@isaudit", DbType.Int32, entity.isaudit);
            cmd.CommandText = sql;
            cmd.CommandType = System.Data.CommandType.Text;
            return DbAccess.ExecuteNonQuery(cmd) > 0;
        }

        public int BatchDelete(string ids)
        {
            string sql = "delete from article where id in(" + ids + ")";
            return DbAccess.ExecuteNonQuery(sql);
        }

        /// <summary>
        /// 批量审核
        /// </summary>
        /// <param name="ids"></param>
        /// <param name="audit"></param>
        /// <returns></returns>
        public int BatchAudit(string ids, int audit)
        {
            string sql = "update article set isaudit = " + audit + " where id in(" + ids + ")";
            return DbAccess.ExecuteNonQuery(sql);
        }

    }
}
